package cn.com.okgo.callback;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;

import cn.com.okgo.cache.TCache;
import okhttp3.Response;
import okhttp3.ResponseBody;

/**
 * Created by luwenlong on 2017/9/5.
 * <p>
 * 返回解析过的数据
 */

public abstract class HttpParsersCallback<T> extends HttpCallback<T> {

    Gson gson = new Gson();
    Type jsonType = new TypeToken<T>() {
    }.getType();

    @Override
    public T parseNetworkResponse(Response response) throws Exception {
        String json = response.body().string();
        TCache.getInstance().put(json, response);
        return gson.fromJson(json, jsonType);
    }

    @Override
    public T parseCacheResponse(ResponseBody responseBody) throws Exception {
        String json = responseBody.string();
        return gson.fromJson(json, jsonType);
    }
}
